package com.xpgk.mvc.statistic.monthNursing;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Record;
import com.xpgk.constant.ConstantSplitPage;
import com.xpgk.dto.SplitPage;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.statistic.customerInfo.CustomerInfoStatisticXLS;
import com.xpgk.mvc.user.User;

public class MonthNursingStatisticController extends  BaseController{
	
	private  MonthNursingStatisticService  monthNursingStatisticService;
	public  void  index()
	{	
		Organization  org=Organization.dao.findById(getCUserOrganIds());
		setAttr("organization", org);
		render("monthNursingStatistic.html");
	}
	
	/**
	 * 月护理表
	 */
	public void getMonthNursing(){
		String organIds = (String) splitPage.getQueryParam().get("organIds");
		if(organIds!=null)
		{
			splitPage.getQueryParam().remove("organIds");
			splitPage.getQueryParam().put("serOrganIds", organIds);
			splitPage.getQueryParam().put("cusOrganIds", organIds);
		}
		String hybh=(String) splitPage.getQueryParam().get("cushybh");
		if(hybh!=null)
		{
			splitPage.getQueryParam().remove("cushybh");
			splitPage.getQueryParam().put("cushybh", String.format("%06d",Integer.parseInt(hybh)));
		}
		String json = paging(splitPage, MonthNursingStatisticSQL.sqlId_splitPageSelect, MonthNursingStatisticSQL.sqlId_splitPageFrom);
		renderJson(json);
	}
	
	public  void  excel()
	{
		SplitPage  splitPage=new SplitPage();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("year", getPara("_query.year"));
		param.put("serOrganIds", getPara("_query.organIds"));
		param.put("cusOrganIds", getPara("_query.organIds"));
		param.put("cusname", getPara("_query.cusname"));
		param.put("cusphone", getPara("_query.cusphone"));
		param.put("cushybh", getPara("_query.cushybh"));
		param.put("isdb", getPara("_query.isdb"));
		param.put("minJzxg", getPara("_query.minJzxg"));
		param.put("maxJzxg", getPara("_query.maxJzxg"));
		param.put("minDelweightnum", getPara("_query.minDelweightnum"));
		param.put("maxDelweightnum", getPara("_query.maxDelweightnum"));
		splitPage.setPageSize(ConstantSplitPage.default_maxSize);
		splitPage.setQueryParam(param);
		String json = paging(splitPage, MonthNursingStatisticSQL.sqlId_splitPageSelect, MonthNursingStatisticSQL.sqlId_splitPageFrom);
		List<Record>  list=(List<Record>)splitPage.getList();
		if(list!=null && list.size()>0)
		{	
			DecimalFormat decimalFormat=new DecimalFormat("0.00");
			for(int i=0;i<list.size();i++)
			{	
				//已减斤数
				float  haveDelWeight=(list.get(i).getFloat("initialweight")==null?0:list.get(i).getFloat("initialweight"))-(list.get(i).getFloat("lastweight")==null?0:list.get(i).getFloat("lastweight"));
				list.get(i).set("haveDelWeight",decimalFormat.format(haveDelWeight) );
				//未减斤数
				float  noDelWeight=(list.get(i).getFloat("lastweight")==null?0:list.get(i).getFloat("lastweight"))-(list.get(i).getFloat("stantardweight")==null?0:list.get(i).getFloat("stantardweight"));
				list.get(i).set("noDelWeight", decimalFormat.format(noDelWeight) );
				//月平均减重斤数
				float  monthAverage=haveDelWeight/12;
				list.get(i).set("monthAverage", decimalFormat.format(monthAverage) );
				//减重效果
				float  jzxg=0;
				if(list.get(i).getFloat("weightloss")!=null)
				{
					jzxg=haveDelWeight/list.get(i).getFloat("weightloss")*100;
				}
				list.get(i).set("jzxg", decimalFormat.format(jzxg)+"%" );
				//是否达到标体
				if(noDelWeight>0)
				{
					list.get(i).set("isdb", "否" );
				}else
				{
					list.get(i).set("isdb", "是" );
				}
			}
		}
		
		//搜索条件
		Organization  organ=Organization.dao.findById(getPara("_query.organIds"));//门店
		String  cusnamestr=getPara("_query.cusname").equals("")?"":(",顾客姓名："+getPara("_query.cusname"));
		String  cusphonestr=getPara("_query.cusphone").equals("")?"":(",顾客手机："+getPara("_query.cusphone"));
		String  cusbhstr=getPara("_query.cushybh").equals("")?"":(",顾客编号："+getPara("_query.cushybh"));
		String  isdbstr="";
		if(getPara("_query.isdb").equals("1")){isdbstr=",是否达到标体：是";}
		if(getPara("_query.isdb").equals("0")){isdbstr=",是否达到标体：否";}
		String  minJzxgstr=getPara("_query.minJzxg").equals("")?"":(",最少减重次数："+getPara("_query.minJzxg"));
		String  maxJzxgstr=getPara("_query.maxJzxg").equals("")?"":(",最多减重次数："+getPara("_query.maxJzxg"));
		String  minDelweightnumstr=getPara("_query.minDelweightnum").equals("")?"":(",最小减重效果："+getPara("_query.minDelweightnum"));
		String  maxDelweightnumstr=getPara("_query.maxDelweightnum").equals("")?"":(",最大减重效果："+getPara("_query.maxDelweightnum"));
		String  search="时间："+getPara("_query.year")+"年,门店："+organ.getStr(Organization.column_name)+
				cusnamestr+cusphonestr+cusbhstr+isdbstr+minJzxgstr+maxJzxgstr+minDelweightnumstr+maxDelweightnumstr+
				",导出人员："+getCUser().getStr(User.column_name);
		render(new MonthNursingStatisticXLS(list,search,"月护理查询表"));
	}
}
